package com.gdkf.dormitory.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdkf.dormitory.entity.Admin;
import com.gdkf.dormitory.entity.MyPage;
import com.gdkf.dormitory.entity.Repair;
import com.gdkf.dormitory.entity.Role;
import com.gdkf.dormitory.service.RoleService;
import com.gdkf.dormitory.tools.MyPageTool;
import com.gdkf.dormitory.vo.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lcy
 * @since 2022-09-09
 */
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("/role/getAllRole")
    public Msg getAllRole(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
//       获得第一次解析的数据
        MyPage myPage = MyPageTool.parseToPage(templateObject);

        Page<Role> page = new Page<>(myPage.getCurrentPage(),myPage.getSizePage());
        roleService.page(page, null);
        List<Role> records = page.getRecords();
        return  records.isEmpty()?Msg.fail():Msg.success()
                .add("roleList",page.getRecords())
                .add("sizePage",page.getSize())
                .add("currentPage",page.getCurrent())
                .add("pageTotal",page.getTotal());
    }

    @PostMapping("/role/getOneRole")
    public Msg getOneRole(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
//       获得第一次解析的数据
        MyPage myPage = MyPageTool.parseToPage(templateObject);
        if(myPage.getObject()!=null){
            MyPage myPage2 = MyPageTool.JsonParseToRole(myPage);
            System.out.println(myPage2);
            Page<Role> page = new Page<>(myPage2.getCurrentPage(),myPage2.getSizePage());
            QueryWrapper<Role> wrapper = new QueryWrapper<>();
            wrapper.eq("type_name",((Role)myPage2.getObject()).getTypeName());
            Page<Role> page1 = roleService.page(page, wrapper);
            List<Role> records = page1.getRecords();
            return records.isEmpty()?Msg.fail():Msg.success()
                    .add("roleList",records)
                    .add("sizePage",page.getSize())
                    .add("currentPage",page.getCurrent())
                    .add("pageTotal",page.getTotal());
        }
        return Msg.fail();
    }
    @PostMapping("/role/addRole")
    public Msg addRole(@RequestBody Role role){
        boolean save = roleService.save(role);
        return save?Msg.success():Msg.fail();
    }

    @PostMapping("/role/deleteRole")
    public Msg deleteRole(@RequestBody Role role){
        System.out.println(role);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("r_id",role.getRId());
        wrapper.eq("type_name",role.getTypeName());
        boolean remove = roleService.remove(wrapper);
        return remove?Msg.success():Msg.fail();
    }

    @PostMapping("/role/updateRole")
    public Msg updateRole(@RequestBody Role role){
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("r_id",role.getRId());
        return roleService.update(role,wrapper)?Msg.success():Msg.fail();
    }
}

